# 双O(n)
class Solution:
    def zeroFilledSubarray(self, nums: List[int]) -> int:
        ret = []
        ans = 0
        temp = []
        n = len(nums)

        # 先找出最大子数组
        for i in range(n):
            if nums[i] == 0:
                temp.append(0)
            if nums[i] != 0 or i == n - 1:
                if temp:
                    ret.append(temp)
                temp = []
        
        # 没有则为0
        if len(ret) == 0:
            return 0
        # 计算全部子数组数量
        for x in ret:
            l = len(x)
            while l > 0:
                ans += l
                l -= 1
        return ans


# 方法一优化
class Solution:
    def zeroFilledSubarray(self, nums: List[int]) -> int:
        n = len(nums)
        ans = 0
        temp = 0
        for i in range(n):
            if nums[i] == 0:
                temp += 1
                ans += temp
            if nums[i] != 0:
                temp = 0
        return ans